<?php
namespace App\Libraries\Decorator\User;


use App\Libraries\Decorator\DecoratorInterface\ModelDecorator;
use App\Libraries\Tools\Point;

class UserPointDecorator implements ModelDecorator
{

    public function before(){
        //todo
    }

    public function after(&$data){
        if(!is_array($data)){
            $data = json_decode(json_encode($data), true);
        }
        if (!isset($data['list'])) {
            if(isset($data['points'])){
                $data['points']['level'] = Point::getLevel($data['points']['total_point']);
                $data['points']['point_to_next'] = Point::pointToNextLevel($data['points']['total_point']);
            }else{
                $data['points']['point'] = 0;
                $data['points']['total_point'] = 0;
                $data['points']['level'] = 1;
                $data['points']['point_to_next'] = 101;
            }
        } else {
            foreach($data['list'] as &$v){
                $this->after($v);
            }
        }
        return $data;
    }
}